﻿using FairyGUI;
using MongoDB.Bson.Serialization.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model
{
    public class BattleCard : AConfig
    {
        public string CardName;
        public string InfoFlag;
        public CardSearchType SearchType;
        [BsonIgnore]
        public BattlePlayer Owner { get; set; }
        [BsonIgnoreIfDefault]
        public CardPlace CardPlace { get; set; }
        [BsonIgnoreIfNull]
        public CardGroup CardGroup { get; set; }
        [BsonIgnore]
        public List<IIfCardUse> IfUseDic { get; set; } = new List<IIfCardUse>();
        [BsonIgnore]
        public Dictionary<string, List<ICardUse>> UseDic { get; set; } = new Dictionary<string, List<ICardUse>>();
        /// <summary>
        /// 横竖
        /// </summary>
        [BsonIgnore]
        public bool IfStand { get; set; }

        /// <summary>
        /// 正反朝向
        /// </summary>
        [BsonIgnore]
        public bool IfRightSide { get; set; }
        public bool IfCenter
        {
            get
            {
                return ((IfStand && CardPlace == CardPlace.Charactor) || CardPlace == CardPlace.Duel||IfHyper);
            }
        }
        public bool IfSupport
        {
            get
            {
                return ((CardPlace == CardPlace.Charactor || CardPlace == CardPlace.Duel|| IfHyper));
            }
        }
        public bool IfHyper
        {
            get
            {
                return (CardPlace == CardPlace.Duel_Charactor);
            }
        }

        public void AddUse(ICardUse cardUse)
        {
            List<ICardUse> list;
            if (!UseDic.TryGetValue(cardUse.UseInfo,out list))
            {
                list = new List<ICardUse>();
                UseDic.Add(cardUse.UseInfo, list);
            }
            list.Add(cardUse);
        }

        /*public T AddComponent<T>() where T : Component, new()
        {
            T t = base.AddComponent<T>();
            return t;
        }*/

        public void Use(FightModel fightModel, BattlePlayer battlePlayer, string UseInfo)
        {
            foreach (var a in UseDic[UseInfo])
            {
                a.Use(fightModel, battlePlayer);
            }
        }

        public async void Use()
        {
            List<string> uselist = IfUse(FightModel.Single, Owner);
            Log.Debug($"{CardName}有种{uselist.Count}使用方法");
            if (uselist.Count == 1)
            {
                Use(FightModel.Single, Owner, uselist[0]);
                await BattleEventWatcherComponent.Single.RunEvent(BattleEventType.FreshUI, FightModel.Single);
            }
            else
            {
                List<DecideInfo> list = new List<DecideInfo>();
                foreach (var a in uselist)
                {
                    list.Add(new DecideInfo()
                    {
                        Describe = a,
                        CallBack = () =>
                        {
                            Use(FightModel.Single, Owner, a);
                        },
                    });
                }
                await BattleEventWatcherComponent.Single.RunEvent(BattleEventType.ShowWindow, FightModel.Single,WindowType.Decide,list);
            }

            await BattleEventWatcherComponent.Single.RunEvent(BattleEventType.FreshUI, FightModel.Single);
        }

        public List<string> IfUse(FightModel fightModel, BattlePlayer battlePlayer)
        {
            List<string> outlist=new List<string>();
            foreach (var a in IfUseDic)
            {
                if (a.IfUse(fightModel, battlePlayer))
                {
                    outlist.Add(a.UseInfo);
                }
            }
            return outlist;
        }
        static string CardPicPath = "CardPic/";

        public static void SetCardToUI(GComponent component, BattleCard battleCard, bool AlwayRightSide = false)
        {
            if (battleCard == null)
            {
                component.icon = "";
                component.data = null;
                return;
            }
            component.icon = CardPicPath + battleCard.InfoFlag;
            if (!battleCard.IfRightSide && !AlwayRightSide) component.icon = CardPicPath + "back";
            component.data = battleCard;
        }

        public void Move(CardGroup AimGroup)
        {
            this.CardGroup.MoveCards(AimGroup, new List<BattleCard>() { this });
        }

        public BattleCard()
        {
        }
        public BattleCard(long id): base(id)
		{
        }
    }
    [Config(AppType.Client)]
    public class CardCategory: ACategory<BattleCard>
    {

    }
}
